package com.cl.edu.web;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.CrudActionSupport;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Area;
import com.cl.edu.entity.Country;
import com.cl.edu.entity.Education;
import com.cl.edu.entity.Highschool;
import com.cl.edu.entity.Status;
import com.cl.edu.entity.Subject;
import com.cl.edu.entity.Teacher;
import com.cl.edu.service.RegisterManager;

@Results( { @Result(name = "login", location = "register-login.jsp", type = "redirect") })
public class RegisterAction extends CrudActionSupport<Teacher>  {
	
	@Autowired
	private RegisterManager registerManager;
	
	private Teacher entity;
	private Long id;
	
	private List<Country> countryList;//国家列表
	private List<Area> shengList; //省列表
	private List<Highschool> hxList;//高校列表
	private List<Area>  syList;//沈阳区划
	private List<Education> eduList;//学历列表
	private List<Status> statusList1;//身份列表1
	private List<Status> statusList2;//身份列表2
	private List<Status> statusList3;//身份列表3
	private List<Subject> subjectList;//教师任教学科列表
	
	/**
	 * 注册步骤1中所需数据列表
	 * @return
	 * @throws Exception
	 */
	public String step1() throws Exception {
		countryList	  = registerManager.getCountries();
		shengList     = registerManager.getSheng();
		hxList        = registerManager.getHighschools();
		syList        = registerManager.getShenYang();
		eduList       = registerManager.geteEducations();
		statusList1   = registerManager.getStatus(1L);
		statusList2   = registerManager.getStatus(2L);
		statusList3   = registerManager.getStatus(3L);
		subjectList   = registerManager.getSubjects("0");
		return SUCCESS;
	}
	
	/**
	 * 注册步骤2中所需数据列表
	 * @return
	 * @throws Exception
	 */
	public String step2() throws Exception {
		
		subjectList   = registerManager.getSubjects("2");
		syList        = registerManager.getShenYang();
		return "two";
	}
	
	public String save() throws Exception {
		entity.setState(1);//刚注册时为未审核状态
		entity.setStep(0);
		entity.setIsWeekStar(0);
		entity.setRegtime(new Date());
		registerManager.saveTeacher(entity);
		Struts2Utils.getSession().setAttribute("username", entity.getUsername()); //在注册成功后登录页面显示用
		return "login";
	}
	
	/**
	 * 检测用户名是否可用
	 * @return
	 * @throws Exception
	 */
	public String check() throws Exception {
		String username   = Struts2Utils.getParameter("username");
		boolean flag = registerManager.isExist(username);
		if(flag) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id == null) {
			entity = new Teacher();
		} else {
			entity = registerManager.getTeacherById(id);
		}
	}

	public Teacher getModel() {
		return entity;
	}


	public Teacher getEntity() {
		return entity;
	}


	public void setEntity(Teacher entity) {
		this.entity = entity;
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public List<Area> getShengList() {
		return shengList;
	}


	public void setShengList(List<Area> shengList) {
		this.shengList = shengList;
	}


	public List<Highschool> getHxList() {
		return hxList;
	}


	public void setHxList(List<Highschool> hxList) {
		this.hxList = hxList;
	}


	public List<Area> getSyList() {
		return syList;
	}


	public void setSyList(List<Area> syList) {
		this.syList = syList;
	}


	public List<Status> getStatusList1() {
		return statusList1;
	}


	public void setStatusList1(List<Status> statusList1) {
		this.statusList1 = statusList1;
	}


	public List<Status> getStatusList2() {
		return statusList2;
	}


	public void setStatusList2(List<Status> statusList2) {
		this.statusList2 = statusList2;
	}


	public List<Status> getStatusList3() {
		return statusList3;
	}


	public void setStatusList3(List<Status> statusList3) {
		this.statusList3 = statusList3;
	}


	public List<Country> getCountryList() {
		return countryList;
	}


	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}


	public List<Education> getEduList() {
		return eduList;
	}


	public void setEduList(List<Education> eduList) {
		this.eduList = eduList;
	}


	public List<Subject> getSubjectList() {
		return subjectList;
	}


	public void setSubjectList(List<Subject> subjectList) {
		this.subjectList = subjectList;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 在step2方法前也需要准备模型
	 * @throws Exception
	 */
	public void prepareStep2() throws Exception	{
		prepareModel();
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
	
}
